package org.scau.studentevaluation.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.scau.studentevaluation.client.AttendedStudentListClient;
import org.scau.studentevaluation.entity.bo.ActBO;
import org.scau.studentevaluation.entity.bo.ConstructorBO;
import org.scau.studentevaluation.entity.dos.ActDO;
import org.scau.studentevaluation.entity.enums.IsCheckedEnum;
import org.scau.studentevaluation.entity.enums.IsPassedEnum;
import org.scau.studentevaluation.entity.enums.IsSubmitedEnum;
import org.scau.studentevaluation.entity.query.ActQuery;
import org.scau.studentevaluation.entity.query.ActUploadQuery;
import org.scau.studentevaluation.entity.vo.ActVO;
import org.scau.studentevaluation.service.IActService;
import org.scau.studentevaluation.service.ImportMapperService;
import org.scau.studentevaluation.tool.DateString;
import org.scau.studentevaluation.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class ActServiceImpl implements IActService{

    private final ImportMapperService importMapperService;

    @Resource
    private final AttendedStudentListClient attendedStudentListClient;

    @Autowired
    public ActServiceImpl(ImportMapperService importMapperService, AttendedStudentListClient attendedStudentListClient){
        this.importMapperService = importMapperService;
        this.attendedStudentListClient = attendedStudentListClient;
    }

    ActDO aDo = new ActDO();

    @Override
    public ActDO getActByActId(Long actId) {
        LambdaQueryWrapper<ActDO> query = Wrappers.<ActDO>lambdaQuery()
                .eq(ActDO::getId, actId)
                .last(" limit 1");
        ActDO AO = importMapperService.actMapper.selectOne(query);
        return AO;
    }

    @Override
    public List<ActVO> getCertificationByIsSubmitedAndOrganization(boolean isSubmited, String organization) {
        LambdaQueryWrapper<ActDO> query = Wrappers.<ActDO>lambdaQuery()
                .eq(ActDO::getIsSubmited, isSubmited)
                .eq(ActDO::getOrganization, organization);
        List<ActDO> DOs = importMapperService.actMapper.selectList(query);
        if(DOs == null){
            return null;
        }
        List<ActBO> BOs = BeanConvertUtils.DOs2BOs(DOs, ActBO.class);
        List<ActVO> VOs = BeanConvertUtils.BOs2VOs(BOs,ActVO.class);
        return  VOs;
    }

    @Override
    public int uploadCertification(ActUploadQuery actQuery) {
        LambdaQueryWrapper<ActDO> query = Wrappers.<ActDO>lambdaQuery()
                .eq(ActDO::getActName,actQuery.getActName())
                .eq(ActDO::getOrganization,actQuery.getOrganization())
                ;
        int count = importMapperService.actMapper.selectCount(query);
        Preconditions.checkArgument(count == 0, "已有同名活动");
        // 把活动插入Act表中
        ActDO actDO = new ActDO();
        actDO.setUploaderId(actQuery.getUploaderId())
                .setOrganization(actQuery.getOrganization())
                .setTime(LocalDateTime.now())
                .setActName(actQuery.getActName())
                .setActTip(actQuery.getActTip())
                .setIsSubmited(IsSubmitedEnum.IS_SUBMITED_FALSE.getType())
                .setIsChecked(IsCheckedEnum.IS_CHECKED_FALSE.getType())
                .setIsPassed(IsPassedEnum.IS_PASSED_FALSE.getType());
        importMapperService.actMapper.insert(actDO);
        // 把参加活动的学生插入AttendedStudentList表中
        Long actId = actDO.getId();
        actQuery.getAttendedStudent().forEach(x -> x.setActId(actId));
        attendedStudentListClient.InsertAttendedStudent(actQuery.getAttendedStudent());
        return 0;
    }

    @Override
    @Transactional
    public int operateCertificationByActId(List<Long> actIds, String operation) {
        switch (operation){
            case "submit":{
                actIds.forEach(id -> {
                    LambdaUpdateWrapper<ActDO> updateWrapper = new LambdaUpdateWrapper();
                    updateWrapper.eq(ActDO::getId, id)
                            .set(ActDO::getIsSubmited,true);
                    importMapperService.actMapper.update(null, updateWrapper);
                });
                break;
            }
            case "delete": {
                actIds.forEach(id -> {
                    importMapperService.actMapper.deleteById(id);
                    attendedStudentListClient.deleteStudentsByActId(id);
                });
                break;
            }
            default:
        }
        return 1;
    }


}
